<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>管理员列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
    <!--
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户</label>
                            <div class="layui-input-inline">
                                <input type="text" name="Username" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        -->

        <table class="layui-table" id="currentTableId" lay-filter="currentTableFilter"></table>

    <!-- 添加弹出框	-->
    	<div class="layui-row" id="AddUserInfodiv" style="display:none;">
      	<div class="layui-col-md11">
        		<form class="layui-form layui-form-pane" action="" lay-filter="AddUserInfo">
  			 	<div class="layui-form-item">
    					<div class="layui-inline">
      					<label class="layui-form-label">用户名</label>
      					<div class="layui-input-inline" style="width: 213px;">
        						<input type="text" name="Username" autocomplete="off" lay-verify="nickname" placeholder="必填:小写英文字母" class="layui-input">
      					</div>
     					</div>

      				<div class="layui-inline">
        					<label class="layui-form-label">中文名</label>
        					<div class="layui-input-inline" style="width: 213px;">
          					<input type="text" name="Nickname" placeholder="" autocomplete="off"  placeholder="中文名" class="layui-input">
        					</div>
      				</div>
      		</div>
    			<div class="layui-form-item">
      				<div class="layui-inline">
        					<label class="layui-form-label">手机</label>
        					<div class="layui-input-inline" style="width: 213px;">
          					<input type="text" name="Telphone" autocomplete="off" lay-verify="phone" placeholder="必填" class="layui-input">
        					</div>
      				</div>
  
     					<div class="layui-inline">
      					<label class="layui-form-label">邮箱</label>
      					<div class="layui-input-inline" style="width: 213px;">
        						<input type="text" name="Email" autocomplete="off" lay-verify="email" placeholder="必填" class="layui-input">
      					</div>
     					</div>
      		</div>
    			<div class="layui-form-item">
     					<div class="layui-inline">
      					<label class="layui-form-label">组织机构</label>
      					<div class="layui-input-inline" style="width: 213px;">
        						<input type="text" name="Company" placeholder="" autocomplete="off" class="layui-input">
      					</div>
     					</div>

     					<div class="layui-inline">
      					<label class="layui-form-label">部门</label>
      					<div class="layui-input-inline" style="width: 213px;">
        						<input type="text" name="Department" placeholder="" autocomplete="off" class="layui-input">
      					</div>
     					</div>              
      		</div>
    			<div class="layui-form-item">  
          			<div class="layui-inline">
  		  				<label class="layui-form-label">角色</label>
             				<div class="layui-input-block" id="RoleCheckBox"></div>
          			</div>
      		</div>
    			<div class="layui-form-item">  
     					<div class="layui-inline">
      					<label class="layui-form-label">密码</label>
      					<div class="layui-input-inline" style="width: 213px;">
        						<input type="password" id="password" name="Password" lay-verify="required" placeholder="必填" autocomplete="off" class="layui-input">
      					</div>
     					</div>
  					
     					<div class="layui-inline">
      					<label class="layui-form-label">确认密码</label>
      					<div class="layui-input-inline" style="width: 213px;">
        						<input type="password" id="L_repass" name="Repassword" lay-verify="required|confirmPass" placeholder="必填" autocomplete="off" class="layui-input">
      					</div>
     					</div>
      		</div>
    			<div class="layui-form-item">  
        				<div class="layui-inline">
            				<label class="layui-form-label">状态</label>
            				<div class="layui-input-block">
              				<select name="Status" lay-filter="StatusKey">
                					<option value="1" selected="">启用</option>
  			  					      <option value="0">禁用</option>
              				</select>
            				</div>
  	  				</div>
  
      				<div class="layui-inline">
        					<label class="layui-form-label">备注</label>
        					<div class="layui-input-inline" style="width: 213px;">
          					<input type="text" name="Remark" autocomplete="off" class="layui-input">
        					</div>
      				</div>
    				</div>
          		<div class="layui-form-item">
            			<button class="layui-btn" lay-submit="" lay-filter="AddUserSubmit">确认添加</button>
          		</div>
        		</form>
      	</div>
    	</div>
    <!-- 添加弹出框	-->	

    </div>
</div>
</body>

<script type="text/html" id="statusTpl">
  {{# if (d.Status == '1') { }}
     <span style="color:#218868">启用</span>
	{{# } else { }}
    <span style="color:#7A8B8B">禁用</span>
{{# } }}
</script>	

<script type="text/html" id="toolbarHead">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="Add"><i class="layui-icon">&#xe61f;</i>添加</button>
    </div>
</script>

<script type="text/html" id="listAction">
    <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="Edit">编辑</a>
    <a class="layui-btn layui-btn-normal layui-btn-sm layui-btn-danger" lay-event="Delete">删除</a>
</script>

<script>
	//加载添加时的角色复选框
    $.ajax({
        url: "/rbac/role/List",
        type: "GET",
		    headers: {'X-Requested-With':'XMLHttpRequest'},
        dataType: "json",
        success: function(resp) {
			    var jsondatas = eval(resp.data);
			    $.each(resp.data, function (i, item) {
				      var html = '<input type="checkbox" name="Roleid" value="' + jsondatas[i].Id + '" title="' + jsondatas[i].Name+'">';
          		$("#RoleCheckBox").append(html);								
			    });	
        }
    });	

    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

            form.render();

      		  form.verify({
        			nickname: function(value){
        			  if(value.length < 2){
        				return '至少5个字符且不能有特殊字符';
        			  }
        			}
        			,confirmPass:function(value){
                if($('input[name=Password]').val() !== value)
                    return '两次输入密码不一致';
              }
      		  });

         //头工具栏事件
          table.on('toolbar(currentTableFilter)', function(obj){
              var checkStatus = table.checkStatus(obj.config.id);
              switch(obj.event){
                case 'Add':							
    		            layer.open({
                    	//layer提供了5种层类型。可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
                        type:1,
            			      skin: 'layui-layer-rim',
                        title:"添加管理员",
                        area: ['50%', '90%'],
                        content:$("#AddUserInfodiv") //content:$("#AddUserInfodiv").html()
                    });				
    		            form.render();	
		                //添加提交
                    form.on('submit(AddUserSubmit)', function(datas){
            		        var arr = new Array();
                        $("input:checkbox[name='Roleid']:checked").each(function(i){
                            arr[i] = $(this).val();
                        });
                        datas.field.Roleid = arr.join(",");//将数组合并成字符串
            
                		   // console.log(datas.field);			
                			   layer.confirm('确定添加?', {icon: 3, title:'提示',yes: function(index){
                                     $.ajax({
                                       url: "/rbac/user/Add",
                                       type: "post",
                                       data: datas.field,
                                       dataType: "json",
                                       success: function (resp) {
                					            console.log(resp);
                                                 if(resp.status){
                                                    layer.msg('添加成功', {icon: 1});
                									                  window.location.reload();
                                                 }else{
                                                    layer.msg('添加失败',{icon:2});
                                                 }
                                        }
                                   });		  	  
                               },
                                cancel: function(index, layero){ 
                                    layer.close(index);
                		            console.log("不操作");
                                } 
                            }); 		   
            	            return false; 	
            		    });			
                break;
                case 'DelUser':
                   var data = checkStatus.data;
                   layer.msg('选中了：'+ data.length + ' 个');
                   break;  
                };
          });	

        table.render({
            elem: '#currentTableId',
            url: '/rbac/user/List'+location.search,
            toolbar: '#toolbarHead',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                //{type: "checkbox", width: 50},
                {field:'Id',width:80,sort: true, title: 'ID'},
      		      {field:'Username',sort: true, title: '用户名'},
                {field:'Telphone',sort: true, title: '电话'},
                {field:'Nickname',sort: true, title: '名字'},
                {field:'Email',hide: true, title: 'Email'},
                {field:'Company',hide: true, title: '组织机构'},
                {field:'Department',sort: true, title: '部门'},
                {field:'Lastlogintime',sort: true, title: '上次登录'},
      		      {field:'Lastloginip',sort: true, title: '登录IP'},
                {field:'Createtime',hide: true, title: '创建时间'},
      		      {field:'Status',sort: true, title: '状态',width:80,templet: '#statusTpl', unresize: true},
      		      {field:'Remark',sort: true, title: '备注'},
                {fixed:'right',align: 'left', toolbar: '#listAction', title: '操作'}
            ]],
            //size:'lg',
            limits: [25, 50, 100],
            limit: 25,
            page: true
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //var result = JSON.stringify(data.field);
            data.field.cronjobName = data.field.cronjobName.replace(/^\s*|\s*$/g,"");
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                },
                where: {
                  key:{
                    cronjobName:data.field.cronjobName
                  }
                }
            });
            return false;
        });

        table.on('tool(currentTableFilter)', function(obj){
          var data = obj.data;
          if(obj.event === 'Edit'){
                var index = layer.open({
                    title: '编辑用户'+data.Username,
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['50%', '92%'],
                    content: '/page/rbac/adminEdit.html?Id='+data.Id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
      	    //xadmin.open('编辑用户'+data.Username,'/rbac/user/UpdateUser?Id='+data.Id,700,800);
          } else if(obj.event === 'Delete'){
              layer.confirm('确定删除?', {icon: 3, title:'删除提示',yes: function(index){
          			  $.ajax({
            			    url: "/rbac/user/Delete",
            			    type: "post",
            			    data: data,
            			    dataType: "json",
            			    success: function (resp) {
            						console.log(resp);
            						 if(resp.status){
            							 layer.msg('删除成功', {icon: 1});
            							 //window.location.reload();
            							 obj.del();
            						 }else{
            							 layer.msg('删除失败',{icon:2});
            						 }
            				  }
          		    });		  	  
                },
            		cancel: function(index, layero){ 
            			layer.close(index);
            			console.log("不操作");
            		} 
      		    }); 
          }
        });

    });
</script>
</html>